package com.woniuxy.three.pms.comment.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.woniuxy.three.pms.bean.CommentBean;
import com.woniuxy.three.pms.comment.service.ICommentService;
import com.woniuxy.three.pms.util.DateUlti;

/**
 * 
 * @ProjectName:Pms223
 * @Date:2019年6月21日
 * @ClassName:CommentController.java
 * @Author:蒋伟
 * @Description:TODO 评论系统而查看，添加，删除方法
 * @Version:版本
 * @See：参考版本
 *
 */

@Controller
public class CommentController {

	@Autowired
	private ICommentService ser;

	/*
	 * 根据时间倒序排列酒店用户的最新评论和综合评分。
	 */
	@ResponseBody
	@GetMapping("/allcomment")
	public List findAll() {

		System.out.println(ser.findAll());
		return ser.findAll();

	}

	/*
	 * 用户根据自己的订单id来查询自己对订单的评论和打分
	 */

	@ResponseBody
	@GetMapping("/comment")
	public CommentBean findByorderId(int order_id) {

		return ser.findByOrderId(order_id);
	}

	/*
	 * 用户添加评论和给酒店打分
	 */
	
	@PostMapping(value = "/comment")
	public String addComment(Model model,ModelMap map,@Valid CommentBean bean,BindingResult result) {
		
		String date = DateUlti.getTime();
		bean.setCreate_time(date);
		ser.addComment(bean);
		if(result.hasErrors()) {
			
			List<FieldError> list = result.getFieldErrors();
			for (FieldError fieldError : list) {
				map.put("error_"+fieldError.getField(), fieldError.getDefaultMessage());
				
			}
			return "/addComment.html";
		}
		
		return "/commentSusscess.html";
	}

	
	/*
	 * 用户删除自己的评论和打分。
	 */
	
	@ResponseBody
	@DeleteMapping(value = "/comment")
	public String delete(int order_id) {
		ser.delete(order_id);
		return "/commentSusscess.html";
	}

}